package com.xwaf.platform.system.provider.config;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.xwaf.platform.common.api.entity.QuartzEntity;

/**
 * 
 * <p>
 * 启动加载相关参数配置
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019年12月27日 下午5:02:34 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@Component
public class InitConfig implements ApplicationRunner {

	private final static Logger LOGGER = LoggerFactory.getLogger(InitConfig.class);
	@Autowired
	private Scheduler scheduler;

	@Override
	public void run(ApplicationArguments var) throws Exception {
		// Configs.init(filePath);
		// 平安银行 初始化配置
		// PABankSDK.init("src/main/resources/config.properties");
		// 平安银行 验证开发者
		// PABankSDK.getInstance().approveDev();
		// 测试定时任务
		// getTest01Task();
	}

	private void getTest01Task()
			throws ClassNotFoundException, InstantiationException, IllegalAccessException, SchedulerException {
		LOGGER.info("初始化测试定时任务");
		QuartzEntity quartz = new QuartzEntity();
		quartz.setJobName("test01");
		quartz.setJobGroup("test");
		quartz.setDescription("测试任务");
		quartz.setJobClassName("com.xwaf.platform.system.provider.task.ChickenJob");
		quartz.setCronExpression("0/20 * * * * ?");
		Class cls = Class.forName(quartz.getJobClassName());
		cls.newInstance();
		// 构建job信息
		JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(), quartz.getJobGroup())//
				.withDescription(quartz.getDescription()).build();
		// 添加JobDataMap数据
		job.getJobDataMap().put("title", "欢迎你");
		job.getJobDataMap().put("blog", "https://gitee.com/xwaf/projects");
		job.getJobDataMap().put("data", new String[] { "张三", "李四" });
		// 触发时间点
		CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression());
		Trigger trigger = TriggerBuilder.newTrigger()
				.withIdentity("trigger" + quartz.getJobName(), quartz.getJobGroup()).startNow()
				.withSchedule(cronScheduleBuilder).build();
		// 交由Scheduler安排触发
		scheduler.scheduleJob(job, trigger);
	}

}